//
//  LTRingEcgDrawHistoryView.m
//  CareRingApp
//
//  Created by Linktop on 2024/4/11.
//  封装ecg画图,用于完整的画出整个波形图

#import "CARingEcgDrawHistoryView.h"
#import "UIView+LayoutDirection.h"

@implementation CARingEcgDrawHistoryView

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self baseLayout];
        
    }
    return self;
}

-(void)baseLayout
{
    [self addSubview:self.contentScrollView];
    [self.contentScrollView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.equalTo(self.contentScrollView.superview);
    }];
    
    [self.contentScrollView addSubview:self.drawLineView];
    [self.contentScrollView addSubview:self.gridView];
    // 画线
    [self.drawLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.drawLineView.superview.mas_leading);
        
        make.top.equalTo(self.drawLineView.superview.mas_top);
        make.height.equalTo(self.gridView.mas_height);
    }];
    // 网格
    [self.gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.gridView.superview.mas_leading);
        make.top.equalTo(self.gridView.superview.mas_top);
        make.trailing.equalTo(self.drawLineView.mas_trailing);
        
        make.height.equalTo(self.gridView.superview.mas_height);

    }];
    
}

-(void)setShowSeconds:(BOOL)showSeconds
{
    _showSeconds = showSeconds;
    self.gridView.showSeconds = _showSeconds;
    if (_showSeconds) {
        CGFloat space = self.gridView.bottomSpaceForSecond;
        // 画线
        [self.drawLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.drawLineView.superview.mas_leading);
            make.top.equalTo(self.drawLineView.superview.mas_top);
            make.bottom.equalTo(self.gridView.mas_bottom).inset(space);
        }];
        // 网格
        [self.gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.drawLineView.superview.mas_leading);
            make.top.equalTo(self.drawLineView.superview.mas_top);
            make.trailing.equalTo(self.drawLineView.mas_trailing);
            make.height.equalTo(self.gridView.superview.mas_height);

        }];
        [self.drawLineView layoutIfNeeded];
        [self.gridView layoutIfNeeded];

    }
    
    if (!_showSeconds) {
        // 画线
        [self.drawLineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.drawLineView.superview.mas_leading);
            make.top.equalTo(self.drawLineView.superview.mas_top);
            make.height.equalTo(self.gridView.mas_height);
        }];
        // 网格
        [self.gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.leading.equalTo(self.drawLineView.superview.mas_leading);
            make.top.equalTo(self.drawLineView.superview.mas_top);
            make.trailing.equalTo(self.drawLineView.mas_trailing);
            make.height.equalTo(self.gridView.superview.mas_height);

        }];
        [self.drawLineView layoutIfNeeded];
        [self.gridView layoutIfNeeded];

    }
    
}


-(void)setHorizonalSpeed:(HORIZONTAL_SPEED)speedType VertialGain:(VERTIAL_GAIN)vGain
{
    [self.drawLineView setHorizonalSpeed:speedType VertialGain:vGain];
    self.gridView.xSpeedType = speedType;
}

-(void)setEcgLineColor:(UIColor *)ecgLineColor
{
    self.drawLineView.ecgLineColor = ecgLineColor;
    
}

-(void)drawAllData:(NSArray<NSNumber *> *)allData
{
    if (!self.ecgRecord) {
        DebugNSLog(@"%s %d:Warning, ecgRecord is nil, which may cause inaccurate drawing length.", __FILE__, __LINE__);
    }
    [self.drawLineView setFreqType:self.ecgRecord.frequencyType == ECG_FREQ_512 ? ECG_DR_LINE_FRQ_512 : ECG_DR_LINE_FRQ_500 ];
    CGFloat width = [self.drawLineView calcHistoryWidth:allData.count];
    self.contentScrollView.contentSize = CGSizeMake(width, self.contentScrollView.bounds.size.height);
//    self.contentScrollView.contentSize = CGSizeMake(width, 0);

//    if (self.showSeconds) {
//        CGFloat space = self.gridView.bottomSpaceForSecond;
//        // 画线
//        [self.drawLineView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.left.equalTo(self.drawLineView.superview.mas_left);
//            make.top.equalTo(self.drawLineView.superview.mas_top);
//            make.width.equalTo(@(width));
//
//            make.bottom.equalTo(self.drawLineView.superview.mas_bottom).inset(space);
//        }];
//        
//    } else {
        CGFloat space = self.gridView.bottomSpaceForSecond;

        [self.drawLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            if (self.directionFromLeft ) {
                make.leading.equalTo(self.drawLineView.superview.mas_leading);
            } else {
                make.trailing.equalTo(self.drawLineView.superview.mas_leading);

            }
            
            make.top.equalTo(self.drawLineView.superview.mas_top);
            make.width.equalTo(@(width));
            if (self.showSeconds) {
                // 缩短高度,避免有秒数的时候线冲出范围
                make.height.mas_equalTo(self.contentScrollView.bounds.size.height - space);
            } else {
                make.height.equalTo(self.drawLineView.superview.mas_height);
            }

        }];
    [self.gridView mas_remakeConstraints:^(MASConstraintMaker *make) {
        
        make.leading.equalTo(self.drawLineView.mas_leading);
        make.top.equalTo(self.gridView.superview.mas_top);
        make.trailing.equalTo(self.drawLineView.mas_trailing);
        
        make.height.equalTo(self.gridView.superview.mas_height);

    }];
        
//    }
    
  
//    [self.drawLineView layoutIfNeeded];

    [self.contentScrollView setNeedsLayout];
    
        
    [self.drawLineView performSelector:@selector(drawAllData:) withObject:allData afterDelay:0.5f];

}

-(void)reset
{
    [self.drawLineView reset];
}



-(UIScrollView *)contentScrollView
{
    if (!_contentScrollView)
    {
        _contentScrollView = [[UIScrollView alloc]init];
        _contentScrollView.showsHorizontalScrollIndicator = YES;
        _contentScrollView.showsVerticalScrollIndicator = NO;
        
    }
    return _contentScrollView;
}

-(LTRingEcgDrawLineView *)drawLineView
{
    if (!_drawLineView) {
        LTRingEcgDrawLineView *lineView = [[LTRingEcgDrawLineView alloc]init];
        lineView.backgroundColor = UIColor.clearColor;//ECG_BACKGROUND_COLOR_DEFAULT;
        //suggest to use HORIZONTAL_SPEED_DEFAULT and VERTIAL_GAIN_DEFAULT
        
//        NSNumber *spType = [AccountCenter instance].currentAccount.otherInfo.ecgXspeedType;
//        NSNumber *ygainType = [AccountCenter instance].currentAccount.otherInfo.ecgYgainType;
//
//        
//        [lineView setHorizonalSpeed:spType.unsignedIntValue VertialGain:ygainType.unsignedIntValue];
        
        lineView.ecgLineColor = ([UIColor colorWithRed:219/255.0f green:64/255.0f blue:70/255.0f alpha:1.f]);
        _drawLineView = lineView;
    }
    
    return _drawLineView;
}

-(LTHmEcgGridView *)gridView
{
    if (!_gridView) {
        _gridView = [[LTHmEcgGridView alloc] init];
    }
    return _gridView;
}

@end
